package com.bossly.metro.Views;

import java.util.Collection;

import com.bossly.metro.Metropoliten;
import com.bossly.metro.dom.Station;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import android.util.AttributeSet;
import android.view.GestureDetector;
import android.view.GestureDetector.OnGestureListener;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ZoomButtonsController;
import android.widget.ZoomButtonsController.OnZoomListener;

public class SchemeView extends View implements OnZoomListener, OnGestureListener {

	public Metropoliten m_metro;
	private GestureDetector m_gesture;
	private ZoomButtonsController m_zoom;

	int m_mapX = 0;
	int m_mapY = 0;
	int m_mapW = 1000;
	int m_mapH = 1000;

	float m_scale = 0.5f;
	private float m_max_scale_out = 0.2f;
	private float m_scale_offset = 0.1f;


	public SchemeView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		// TODO Auto-generated constructor stub
	}

	public SchemeView(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub
	}

	public SchemeView(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
	}
	
	@Override
	protected void onAttachedToWindow() {
		m_zoom = new ZoomButtonsController(this);
		m_zoom.setFocusable(true);
		m_zoom.setAutoDismissed(true);
		m_zoom.setZoomInEnabled(true);
		m_zoom.setZoomOutEnabled(true);

		m_zoom.setOnZoomListener(this);

		
		m_gesture = new GestureDetector(this);

		// TODO Auto-generated method stub
		super.onAttachedToWindow();
	}
	
	@Override
	public boolean dispatchTouchEvent(MotionEvent event) {
		m_gesture.onTouchEvent(event);

		super.dispatchTouchEvent(event);

		return true;
	}

	
	@Override
	protected void onDraw(Canvas canvas) {

		if( m_metro != null )
		{
			canvas.save();
			canvas.scale(m_scale, m_scale );
			Collection<Station> stations = m_metro.m_stations.values();
			
			// draw lines
			for(Station station : stations)
			{
				if( station.next < 0 )
					continue;
				
				// find dest station
				Station dest = m_metro.getStation(station.next);
				
				Point pt = station.getSreenLocation();
				Point pt2 = dest.getSreenLocation();

				Paint paint = new Paint();
				paint.setColor(Color.WHITE);

				canvas.drawLine(pt.x - m_mapX, pt.y - m_mapY, pt2.x - m_mapX, pt2.y - m_mapY, paint);
			}
			
			// draw stations
			for(Station station : stations)
			{
				Point pt = station.getSreenLocation();
				Paint paint = new Paint();
				paint.setColor(Color.WHITE);
			
				int x = pt.x - m_mapX;
				int y = pt.y - m_mapY;
			
				// draw station
				canvas.drawCircle(x, y, 5, paint);
				// draw name
				canvas.drawText(station.getName(), x + station.tx, y + station.ty, paint);
			}
			
			canvas.restore();
		}
		
		super.onDraw(canvas);
	}

	/* OnGestureListener */
	
	@Override
	public boolean onDown(MotionEvent e) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
			float velocityY) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public void onLongPress(MotionEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
			float distanceY) {
		
		int width = (int) (m_scale * m_mapW - getWidth());
		int height = (int) (m_scale * m_mapH - getHeight());

		if (width > 0)
			m_mapX = (int) Math.min(Math.max(0, m_mapX + distanceX), width);

		if (height > 0)
			m_mapY = (int) Math.min(Math.max(0, m_mapY + distanceY), height);

		invalidate();
		
		return true;
	}

	@Override
	public void onShowPress(MotionEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public boolean onSingleTapUp(MotionEvent e) {
		// TODO Auto-generated method stub
		m_zoom.setVisible(true);

		return false;
	}
	
	/* OnZoomListener */

	@Override
	public void onVisibilityChanged(boolean visible) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onZoom(boolean zoomIn) {
		if (zoomIn) {
			m_scale = Math.min(5f, m_scale + m_scale_offset);
		} else {
			m_scale = Math.max(m_max_scale_out, m_scale - m_scale_offset);
		}

//		m_zoom.setZoomInEnabled(m_scale < 1f);
//		m_zoom.setZoomOutEnabled(m_scale > m_max_scale_out);

		invalidate();
	}
}
